﻿using BeerXMLWPF.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BeerXMLWPF
{
    /// <summary>
    /// Interaction logic for EditHop.xaml
    /// </summary>
    public partial class EditHop : Window
    {
        List<string> lstCombobox;
        public bool OK;
        public EditHop()
        {
            InitializeComponent();
            lstCombobox = new List<string>();
            lstCombobox.Add("Boil");
            lstCombobox.Add("Dry Hop");
            lstCombobox.Add("Mash");
            lstCombobox.Add("First Wort");
            lstCombobox.Add("Arona");
            cbUse.ItemsSource = lstCombobox;
            LoadContent("new");
        }
        public EditHop(string s)
        {
            InitializeComponent();
            lstCombobox = new List<string>();
            lstCombobox.Add("Boil");
            lstCombobox.Add("Dry Hop");
            lstCombobox.Add("Mash");
            lstCombobox.Add("First Wort");
            lstCombobox.Add("Arona");
            cbUse.ItemsSource = lstCombobox;
            LoadContent(s);
        }

        private void LoadContent(string s)
        {
            if (s == "new")
            {
                Global.hop = new Hop();
                cbUse.SelectedItem = "Boil";
            }
            else
            {
                txtName.Text = Global.hop.Name;
                txtAlpha.Text = Global.hop.Alpha;
                txtAmount.Text = Global.hop.Amount;
                cbUse.SelectedItem = Global.hop.Use;
                txtTime.Text = Global.hop.Time;
            }
           
        }
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (txtName.Text == "")
            {
                MessageBox.Show("Please enter hop name");
                return;
            }
            try
            {
                double amount = double.Parse(txtAmount.Text);
                if (amount <= 0)
                {
                    MessageBox.Show("Amount must be a double and greater than 0");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Amount must be a double and greater than 0");
                return;
            }
            try
            {
                double time = double.Parse(txtTime.Text);
                if (time <= 0)
                {
                    MessageBox.Show("Time must be a integer and greater than 0");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Time must be a integer and greater than 0");
                return;
            }
            try
            {
                double alpha = double.Parse(txtAlpha.Text);
                if (alpha <= 0 || alpha >= 100)
                {
                    MessageBox.Show("Alpha must be a double and in bettween 0 and 100");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Alpha must be a double and in bettween 0 and 100");
                return;
            }

            Global.hop.Name = txtName.Text;
            Global.hop.Alpha = double.Parse(txtAlpha.Text).ToString();
            Global.hop.Amount = double.Parse(txtAmount.Text).ToString();
            Global.hop.Display_Amount = Global.hop.Amount + " kg";
            Global.hop.Time = txtTime.Text;
            Global.hop.Use = cbUse.SelectedItem.ToString();
            Global.hop.Version = "1";
            Global.OK = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
